import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import store from "../store";
import { trackApi } from "@xmon/bury";
const service: any = axios.create({
  baseURL: "http://127.0.0.1:5173/", // url = base url + request url
  timeout: 5000,

  // withCredentials: true // send cookies when cross-domain requests
}); //为了不需要在每个页面中都引用axios 并且再端口发生改变时只需要在第5行修改端口号即可
// 提醒 @xmon/bury 监听 axios示例 发出请求的行为
trackApi(service);
// Request interceptors
service.interceptors.request.use(
  (config: AxiosRequestConfig<any>) => {
    // Add X-Access-Token header to every request, you can add other custom headers here
    // if (UserModule.token) {
    //   config.headers['X-Access-Token'] = UserModule.token
    // }
    // config.headers = { Authorization: store.state.token };
    return config;
  },
  (error: any) => {
    Promise.reject(error);
  }
); //请求

// Response interceptors
service.interceptors.response.use(
  (response: AxiosResponse<any, any>) => {
    const res = response.data;
    return res;
  },
  (error: any) => {
    // ElMessage({
    //   message: error.message,
    //   type: 'error',
    //   duration: 5 * 1000
    // })
    return Promise.reject(error);
  }
); //响应

export default service;
